﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using System.Reflection;
using Shpm.System.Web.Control;
using log4net;
using Shpm.System.Validator;

public partial class App_Input_PersonOverTime : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Context.Items["OVERTIME_ID"] != null)
            {
                ViewState["OVERTIME_ID"] = Context.Items["OVERTIME_ID"];

            }
            else
            {
                ViewState["OVERTIME_ID"] = null;
            }
            //ViewState["OVERTIME_ID"] = 1;
            initDic();
            if (Context.Items.Contains("CMB_GROUP_ID"))
            {
                this.ViewState["GROUP_ID"] = Convert.ToString(Context.Items["CMB_GROUP_ID"]);
            }
            if (Context.Items.Contains("CMB_JOB_ID"))
            {
                this.ViewState["JOB_ID"] = Convert.ToString(Context.Items["CMB_JOB_ID"]);
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_ID"))
            {
                this.ViewState["EMPLOYEE_ID"] = Convert.ToString(Context.Items["CMB_EMPLOYEE_ID"]);
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_NAME"))
            {
                this.ViewState["EMPLOYEE_NAME"] = Convert.ToString(Context.Items["CMB_EMPLOYEE_NAME"]);
            }
            if (Context.Items.Contains("CMB_BEGIN_DATE"))
            {
                this.ViewState["BEGIN_DATE"] = Convert.ToString(Context.Items["CMB_BEGIN_DATE"]);
            }
            if (Context.Items.Contains("CMB_END_DATE"))
            {
                this.ViewState["END_DATE"] = Convert.ToString(Context.Items["CMB_END_DATE"]);
            }

        }
    }

    void Page_PreRender(object sender, EventArgs e)
    {
        Button1.Enabled = false;
        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        string emId = manager.getSysUserBean().getMstSysUserBean().EmployeeId;
        PersonEmployeeBean logonBean = new PersonEmployeeBean();
        PersonEmployeeDao logonDao = new PersonEmployeeDao();
        logonBean.EmployeeId = emId;
        logonBean = logonDao.Find(logonBean);
        hfEmployeeId.Value = emId;
        if (ViewState["OVERTIME_ID"] != null)
        {

            PersonOvertimeBean poBean = new PersonOvertimeBean();
            poBean.OvertimeId = Convert.ToInt32(ViewState["OVERTIME_ID"]);
            PersonOvertimeDao poDao = new PersonOvertimeDao();
            poBean = poDao.Find(poBean);
            if (poBean != null)
            {
                OvertimeType.SelectedValue = poBean.OvertimeType;
                OvertimeMemo.Text = poBean.OvertimeMemo;
                BeginDate.Text = poBean.BeginDate == null ? "" : DateUtils.convertToString((DateTime)poBean.BeginDate);
                EndDate.Text = poBean.EndDate == null ? "" : DateUtils.convertToString((DateTime)poBean.EndDate);
                BeginTime.Text = poBean.BeginTime;
                EndTime.Text = poBean.EndTime;
                LeaveDay.Text = poBean.LeaveDay;
                LeaveHour.Text = poBean.LeaveHour;
                hfEmployeeId.Value = poBean.EmployeeId;
                PersonEmployeeBean dataBean = new PersonEmployeeBean();
                dataBean.EmployeeId = poBean.EmployeeId;
                dataBean = logonDao.Find(dataBean);
                this.EmployeeId.Text = dataBean.EmployeeName;
                this.group.Text = dataBean.GroupName;
                this.job.Text = dataBean.JobName;
                if (poBean.EmployeeId == emId)
                {
                    this.Button1.Enabled = true;
                }
            }


        }
        else
        {
            if (logonBean != null)
            {

                this.EmployeeId.Text = logonBean.EmployeeName;
                this.group.Text = logonBean.GroupName;
                this.job.Text = logonBean.JobName;
                Button1.Enabled = true;
            }
        }
    }
    /*数据字典数据初始化*/
    private void initDic()
    {

        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        CommonDictionaryDao dao = new CommonDictionaryDao();
        /*申请假别*/
        paramBean.Catalog = "T_OEVERTIME.OVERTIME_TYPE";
        initAll(dao, paramBean, this.OvertimeType);
    }

    private void initAll(CommonDictionaryDao dao, CommonDictionaryBean paramBean, ListControl obj)
    {
        IList list = null;
        list = dao.FindByCatalog(paramBean);

        obj.DataSource = list;
        obj.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ComValidator1.DoValidator())
        {
            ComValidateChecker vd = new ComValidateChecker();
            if (!string.IsNullOrEmpty(BeginTime.Text))
            { vd.Add(new CheckMatches(BeginTime.Text, Constants.CHECK_REGEX_TIME_HOURMINUTE, new ComMessage("E0034", "开始时间"))); }
            
            if (!string.IsNullOrEmpty(EndTime.Text))
            { vd.Add(new CheckMatches(EndTime.Text, Constants.CHECK_REGEX_TIME_HOURMINUTE, new ComMessage("E0034", "结束时间"))); }
            if (!vd.DoValidator())
            {
                ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                return;
            }
            if (this.BeginDate.Text.Equals(this.EndDate.Text))
            {
                if (this.BeginTime.Text.CompareTo(this.EndTime.Text) > 0)
                {
                    string ErrMessage = "结束时间不能小于开始时间";
                    ((Design_MasterPage)this.Master).InfoMessage = ErrMessage;
                    return;
                }

            }
            ViewState["STATUS"] = "Add";
            SaveBase();
        }
    }

    private void SaveBase()
    {
        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        string emId = manager.getSysUserBean().getMstSysUserBean().EmployeeId;

        PersonOvertimeBean dataBean = new PersonOvertimeBean();
        PersonOvertimeDao Dao = new PersonOvertimeDao();
        dataBean.EmployeeId = hfEmployeeId.Value;
        dataBean.RequestId = hfEmployeeId.Value;
        dataBean.OvertimeType = OvertimeType.SelectedValue;
        dataBean.OvertimeMemo = OvertimeMemo.Text;
        dataBean.BeginDate = DateUtils.convertToDate(this.BeginDate.Text);
        dataBean.EndDate = DateUtils.convertToDate(this.EndDate.Text);
        dataBean.BeginTime = BeginTime.Text;
        dataBean.EndTime = EndTime.Text;
        dataBean.LeaveDay = LeaveDay.Text;
        dataBean.LeaveHour = LeaveHour.Text;
        dataBean.UserId = userId;
        if (ViewState["OVERTIME_ID"] != null)
        {
            if (Convert.ToString(ViewState["STATUS"]) == "Add")
            {
                dataBean.CommitFlg = false;
                dataBean.OvertimeId = Convert.ToInt32(ViewState["OVERTIME_ID"]);
                Dao.Update(dataBean);

            }
        }
        else
        {
            ViewState["OVERTIME_ID"] = (int)Dao.Insert(dataBean);

        }
        ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        string directUrl = "~/App/Input/PersonOvertimeSrhForm.aspx";
        Context.Items["ParamPageIndex"] = this.ViewState["ParamPageIndex"];
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["JOB_ID"]);
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["GROUP_ID"]);
        Context.Items["CMB_EMPLOYEE_ID"] = Convert.ToString(this.ViewState["EMPLOYEE_ID"]);
        Context.Items["CMB_EMPLOYEE_NAME"] = Convert.ToString(this.ViewState["EMPLOYEE_NAME"]);
        Context.Items["CMB_BEGIN_DATE"] = Convert.ToString(this.ViewState["BEGIN_DATE"]);
        Context.Items["CMB_END_DATE"] = Convert.ToString(this.ViewState["END_DATE"]);
        ControllerUtils.Transfer(this.Context, directUrl, false);
    }
}
